<?php

//demo  get_flash_video('http://v.youku.com/v_show/id_XNDg1MjA0ODg=.html');



function get_flash_video($url)
{
    $url_info = parse_url($url);
    preg_match("/(youku.com|ku6.com|sina.com.cn|tudou.com)$/i", $url_info['host'], $hosts);
    $a = video_getflash($url, $hosts[1]);
	$b = video_getflashinfo($url, $hosts[1]);
	if (!$b) {
		return array();
	}
	return $video_data = array(
		'flashvar'=>$a,
		'flashimg'=>$b['img'],
		'host'=>'youku.com',
		'source'=>$url,
		'title'=>$b['title']
	);
}


function video_getflash($link, $host) {
        $return = '';
        if('youku.com' == $host) {
                // http://v.youku.com/v_show/id_XNDg1MjA0ODg=.html
                preg_match_all("/id\_(\w+)[=.]/", $link, $matches);
                if(!empty($matches[1][0])) {
                        $return = $matches[1][0];
                }
        } elseif('ku6.com' == $host) {
                // http://v.ku6.com/show/bjbJKPEex097wVtC.html
                preg_match_all("/\/([\w\-]+)\.html/", $link, $matches);
                if(1 > preg_match("/\/index_([\w\-]+)\.html/", $link) && !empty($matches[1][0])) {
                        $return = $matches[1][0];
                }
        } elseif('sina.com.cn' == $host) {
                // http://you.video.sina.com.cn/b/16776316-1338697621.html
                preg_match_all("/\/(\d+)-(\d+)\.html/", $link, $matches);
                if(!empty($matches[1][0])) {
                        $return = $matches[1][0];
                }
        } elseif('tudou.com' == $host) {
        // http://www.tudou.com/programs/view/oOrUqgezkOc/
        preg_match_all("/\/([\w\-]+)\/*$/", $link, $matches);
        if(!empty($matches[1][0])) {
            $return = $matches[1][0];
        }
    }
        return $return;
}

function video_getflashinfo($link, $host) {
        $return='';
        $content = file_get_contents($link);//获取
        if('tudou.com' == $host)
        {
            //             preg_match_all("/<span class=\"s_pic\">(.*?)<\/span>/i",$content,$img);
            //             preg_match_all("/<title>(.*?)<\/title>/i",$content,$title);
            // $title[1][0] = iconv("GBK","UTF-8",$title[1][0]);
            preg_match_all("/thumbnail\s*=\s*[\"'](http:\S+)[\"']/i",$content,$img);
            preg_match_all("/<title>(.*?)<\/title>/i",$content,$title);
			print_r($img);exit;
            $title[1][0] = iconv("GBK","UTF-8",$title[1][0]);
        }
        elseif('youku.com' == $host)
        {
        	preg_match_all("/\+0800\|(.*?)\|\">/i",$content,$img);
        	preg_match_all("/<title>(.*?)<\/title>/i",$content,$title);
        //$img = $img[1][0];
        }
        elseif('ku6.com' == $host)
        {
        	preg_match_all("/<span class=\"s_pic\">(.*?)<\/span>/i",$content,$img);
        	preg_match_all("/<title>(.*?)<\/title>/i",$content,$title);
        	$title[1][0] = iconv("GBK","UTF-8",$title[1][0]);
        }
        elseif('sina.com.cn' == $host)
        {
        	preg_match_all("/<span class=\"s_pic\">(.*?)<\/span>/i",$content,$img);
        	preg_match_all("/<title>(.*?)<\/title>/i",$content,$title);
        	$title[1][0] = iconv("GBK","UTF-8",$title[1][0]);
        }

        $return['img']   = $img[1][0];
        $return['title'] = $title[1][0];
        return $return;
}

?>